<?php

namespace App\Models\Wx;

use App\Http\Requests\Request;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Redis as Redis;

class WxCustomer extends Model
{

    protected $table = 'wechat_customer';

    protected $primaryKey = 'openId';

    public $timestamps = false;

    public $incrementing = false;

    /**
     * 查询粉丝信息
     * @param $pcustID
     * @return mixed
     */
    public static function getCache( $pcustID ) {

        $wx_customer = redisTransformation( Redis::get('WDH_WX_INFO_' . $pcustID) );
        if ( !empty($wx_customer ) ) {
            return $wx_customer;
        }

        $wx_customer = self::where('pcustID',$pcustID)->first();
        if ( empty( $wx_customer ) ) {
            return [];
        }

        $wx_customer = $wx_customer->toArray();

        $redis_data = [
            'pcustID' => $wx_customer['pcustID'],
            'headPic' => $wx_customer['headPic'],
            'nickName' =>$wx_customer['nickName'],
            'openId' =>$wx_customer['openId']
        ];

        Redis::setex('WDH_WX_INFO_' . $pcustID, 604800, redisTransformation( $redis_data, 'set', 604800 ) );

        return $redis_data;

    }

}
